package clientSide.javaGui;

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import org.json.simple.JSONObject;

import static serverSide.Server.*;
import serverSide.GameController;
import clientSide.Player;
import clientSide.javaGui.GamePanels.ChatPanel;
import clientSide.javaGui.GamePanels.TictactoiPanel;

public class GamePage extends JFrame {
	Image bg;
	Player player;
	JSONObject gameProperties;
	JPanel gamePanel,chatPanel;
	String playerName2;
	
	public GamePage(Rectangle r, Image img, Player p,JSONObject gameProperties) {
		this.bg = img;
		this.player = new Player(p);
		this.gameProperties = gameProperties;
		
		this.setTitle("Iran Game Center");
		this.setContentPane(new JComponent() {
			@Override
			protected void paintComponent(Graphics g) {
				g.drawImage(bg,0,0,getParent().getWidth()+15,getParent().getHeight(),
						0,0,getParent().getWidth(),getParent().getHeight(),null);
				super.paintComponent(g);
			}
		});
		this.setLayout(null);
		
		JPanel tmpPanel = new JPanel(new FlowLayout());
		JLabel tmpLabel = new JLabel((String)gameProperties.get(SUBJECT_GAMEREQUEST_KEY_PLAYER1));
		tmpLabel.setForeground(Color.RED);
		tmpLabel.setOpaque(false);
		tmpPanel.add(tmpLabel);
		tmpLabel = new JLabel("          VS          ");
		tmpLabel.setOpaque(false);
		tmpPanel.add(tmpLabel);
		tmpLabel = new JLabel((String)gameProperties.get(SUBJECT_GAMEREQUEST_KEY_PLAYER2));
		tmpLabel.setOpaque(false);
		tmpLabel.setForeground(Color.RED);
		tmpPanel.add(tmpLabel);
		tmpPanel.setBounds(30, 30, 600, 100);
		tmpPanel.setOpaque(false);
		this.add(tmpPanel);
		
		switch ((int)gameProperties.get(SUBJECT_GAMEREQUEST_KEY_TYPE)) {
		case SUBJECT_GAMEREQUEST_VALUE_TICTACTOI:
			if(player.getName().equals((String)gameProperties.get(SUBJECT_GAMEREQUEST_KEY_PLAYER1))) {
				gamePanel = new GamePanels.TictactoiPanel(true);
				playerName2 = (String)gameProperties.get(SUBJECT_GAMEREQUEST_KEY_PLAYER2);
			} else {
				gamePanel = new GamePanels.TictactoiPanel(false);
				playerName2 = (String)gameProperties.get(SUBJECT_GAMEREQUEST_KEY_PLAYER1);
			}
			break;
		default:
			break;
		}
		gamePanel.setBounds(30, 130, 400, 400);
		add(gamePanel);
		
		Thread t = new Thread(new Runnable() {
			@Override
			public void run() {
				reciveAction();
			}
		});
		t.start();
		
		chatPanel = new GamePanels.ChatPanel(player.getName());
		chatPanel.setBounds(500, 130, 250, 380);
		add(chatPanel);
		
		JButton tmpButton = new JButton("complain");
		tmpButton.setBounds(30, 560, 110, 30);
		tmpButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				String ans = JOptionPane.showInputDialog(getParent(),new String("Enter your complain."),"");
			}
		});
		add(tmpButton);
		
		this.setResizable(true);
		this.setBounds(r);
		this.setVisible(true);
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
	}
	
	public void sendAction(JSONObject j) {
		try {
			player.getObjectOutputStream().writeObject(j);
		} catch(Exception ex) {
			ex.printStackTrace();
		}
	}
	
	public void reciveAction() {
		while(true) {
			try {
				Object obj;
				if((obj = player.getObjectInputStream().readObject()) != null) {
					JSONObject json = (JSONObject)obj;
					if((int)json.get(SUBJECT_KEY) == SUBJECT_VALUE_GAMING) {
						if((int)json.get(SUBJECT_GAMING_KEY_ACTION) == SUBJECT_GAMING_VALUE_GAME)
							((TictactoiPanel)gamePanel).reciveChoice((int)json.get(SUBJECT_GAMING_KEY_GAMEACTION));
						else if((int)json.get(SUBJECT_GAMING_KEY_ACTION) == SUBJECT_GAMING_VALUE_CHAT) {
							((ChatPanel)chatPanel).recive((String)json.get(SUBJECT_GAMING_VALUE_CHAT), playerName2);
						} else {
							if(!player.getName().equals((String)json.get(SUBJECT_GAMING_KEY_WINNER)))
								((TictactoiPanel)gamePanel).reciveChoice((int)json.get(SUBJECT_GAMING_KEY_GAMEACTION));
							JOptionPane.showMessageDialog(this, "Winner is : "+(String)json.get(SUBJECT_GAMING_KEY_WINNER)
									, "Winner",JOptionPane.INFORMATION_MESSAGE);
							new ProfilePage(this.getBounds(), bg, player.getSocket(), player.getObjectOutputStream(),
									player.getObjectInputStream(), player.getName());
							this.dispose();
						}
					}
				}
			} catch(Exception ex) {
				ex.printStackTrace();
			}
		}
	}
}
